package com.example.sm.mycook.activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;

import com.example.sm.mycook.R;
import com.example.sm.mycook.adapter.HomeComboAdapter;
import com.example.sm.mycook.beans.HomeComboBean;
import com.example.sm.mycook.interfaceCallBack.HomeComboCallBack;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class HomeComboActivity extends AppCompatActivity {

    private ListView combo_lv;
    private HomeComboAdapter adapter;
    private List<HomeComboBean.DataBean> list=new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home_combo);
        combo_lv = ((ListView) findViewById(R.id.combo_lv));
        initLoadData();




    }

    //http://api5.wochu.cn/client/v1/goods/package/list
    private void initLoadData() {

        Retrofit retrofit=new Retrofit.Builder().baseUrl("http://api5.wochu.cn/client/").addConverterFactory(GsonConverterFactory.create()).build();

        HomeComboCallBack homeComboCallBack=retrofit.create(HomeComboCallBack.class);

        homeComboCallBack.getData().enqueue(new Callback<HomeComboBean>() {
            @Override
            public void onResponse(Call<HomeComboBean> call, Response<HomeComboBean> response) {

                List<HomeComboBean.DataBean> dataBeanList=response.body().getData();

                for (int i = 0; i < dataBeanList.size(); i++) {

                    HomeComboBean.DataBean dataBean=dataBeanList.get(i);
                    list.add(dataBean);
                }
                adapter=new HomeComboAdapter(HomeComboActivity.this,list);
                combo_lv.setAdapter(adapter);
                getListViewHeight(combo_lv);
                initLvClick();

            }

            @Override
            public void onFailure(Call<HomeComboBean> call, Throwable t) {
                Log.e("BBB","数据下载失败");
            }
        });
    }

    private void initLvClick() {
        combo_lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(HomeComboActivity.this,"你点击了====》"+position,Toast.LENGTH_SHORT).show();
            }
        });
    }


    private void getListViewHeight(ListView lv)
    {
        ListAdapter listAdapter=lv.getAdapter();
        if (listAdapter==null)
        {
            return;
        }
        int totalHeight =0;
        for (int i = 0; i < listAdapter.getCount(); i++) {

            View listItem=listAdapter.getView(1,null,lv);
            listItem.measure(0,0);
            totalHeight+=listItem.getMeasuredHeight();

        }
        ViewGroup.LayoutParams params=lv.getLayoutParams();
        params.height=totalHeight+(lv.getDividerHeight()*(listAdapter.getCount()-1));

        lv.setLayoutParams(params);

    }

    public void BackClick(View view) {
        finish();
    }
}
